/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dameki.output;

import dameki.game.Stone;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *
 * @author crazysaem1337
 */
public class JFrameExt extends JFrame 
{    
    private BufferedImage background = null;
    
    private BufferedImage white = null;
    private BufferedImage whiteKing = null;
    
    private BufferedImage black = null;
    private BufferedImage blackKing = null;
    
    private Stone[][] field;
    
    private Stone animatedStone = null;

    Rectangle rect = new Rectangle(0, 0, 100, 50);

    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);

    Graphics2D big;

    boolean firstTime = true; //It's my first time be gentle .. *um* .. *yeah* ..

    Rectangle area;
    
    public void setAnimatedStone(Stone animatedStone) {
        this.animatedStone = animatedStone;
    }

    public void setField(Stone[][] field) {
        this.field = field;
    }
    
    public JFrameExt(String title)
    {
        super(title);
        
        try {
            background = ImageIO.read(new File("src/assets/board.png"));
            
            white = ImageIO.read(new File("src/assets/white.png"));
            whiteKing = ImageIO.read(new File("src/assets/white_king.png"));
            
            black = ImageIO.read(new File("src/assets/black.png"));
            blackKing = ImageIO.read(new File("src/assets/black_king.png"));
        } catch (IOException ex) {
            Logger.getLogger(JFrameExt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) this.getContentPane().getGraphics();
        
        if (firstTime) {
            Dimension dim = getSize();
            int w = dim.width;
            int h = dim.height;
            area = new Rectangle(dim);
            bi = (BufferedImage) createImage(w, h);
            big = bi.createGraphics();
            rect.setLocation(w / 2 - 50, h / 2 - 25);
            big.setStroke(new BasicStroke(8.0f));
            firstTime = false;
        }

        big.setColor(Color.white);
        big.clearRect(0, 0, area.width, area.height);

        big.setPaint(Color.red);
        big.draw(rect);
        big.setPaint(Color.blue);
        big.fill(rect);
        
        big.drawImage(background, 0, 0, this);
        
        if(field == null)
        {
            return;
        }
        
        for (int x = 7; x >= 0; x--) {
            for (int y = 0; y < 8; y++) {
                if (field[y][x] == null) {
                } else if (field[y][x].getColor()) {
                    if (field[y][x].IsDame()) {
                        big.drawImage(whiteKing, x*75, (7-y)*75, null);
                    } else {
                        big.drawImage(white, x*75, (7-y)*75, null);
                    }
                } else {
                    if (field[y][x].IsDame()) {
                        big.drawImage(blackKing, x*75, (7-y)*75, null);
                    } else {
                        big.drawImage(black, x*75, (7-y)*75, null);
                    }
                }
            }
        }
        
        if(animatedStone != null)
        {
            //System.out.println("x: " + animatedStone.getPosX() + " y: " + animatedStone.getPosY());
            if(animatedStone.getColor())
            {
                if(animatedStone.IsDame())
                {
                    big.drawImage(whiteKing, animatedStone.getPosX(), animatedStone.getPosY(), null);
                }
                else
                {
                    big.drawImage(white, animatedStone.getPosX(), animatedStone.getPosY(), null);
                }
            }
            else
            {
                if(animatedStone.IsDame())
                {
                    big.drawImage(blackKing, animatedStone.getPosX(), animatedStone.getPosY(), null);
                }
                else
                {
                    big.drawImage(black, animatedStone.getPosX(), animatedStone.getPosY(), null);
                }
            }
        }
        
        g2.drawImage(bi, 0, 0, this);
    }
}
